<?php

//這個是客戶機器上面的PHPMailer的引用路徑。
//include("libraries/phpmailer/phpmailer.php"); //匯入PHPMailer類別

include("PHPMailer/class.phpmailer.php"); //匯入PHPMailer類別
include("readCsv.php"); //匯入讀取商品csv檔的函式。

	/*
	 * 擺脫"magic_quotes_gpc"為開啟時造成的影響。
	 */
	if (get_magic_quotes_gpc())
	{
	    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
	    while (list($key, $val) = each($process)) {
	        foreach ($val as $k => $v) {
	            unset($process[$key][$k]);
	            if (is_array($v)) {
	                $process[$key][stripslashes($k)] = $v;
	                $process[] = &$process[$key][stripslashes($k)];
	            } else {
	                $process[$key][stripslashes($k)] = stripslashes($v);
	            }
	        }
	    }
	    unset($process);
	}
	
	/*
	 * 填入『裸包袋茶』的商品資料，如果消費者沒有購買的商品就不要顯示出來。
	 */
	$html_teaGroup1 = '';
	$html_teaGroup1_content = '';
	//此讀取csv檔的函式被定義在readCsv.php裡頭。
	$ary_products_teaGroup1 = getFromCSV('cartree_products_group1.csv');
	for($i=1 ; $i<=8 ; $i++)
	{
		if(! empty($_POST['length1-' . $i]))
		{
			$html_teaGroup1_content .= '<tr align="center">';
			$html_teaGroup1_content .= '<td align="left"  style="padding-left:47px;">' . $ary_products_teaGroup1[$i-1][0] . '</td>';
			$html_teaGroup1_content .= '<td class="money">' . $ary_products_teaGroup1[$i-1][1] . '</td>';
			$html_teaGroup1_content .= '<td><input type="text" name="length1-1" class="length" value="' . $_POST['length1-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup1_content .= '<td><input type="text" name="price1-1" class="price" value="' . $_POST['price1-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup1_content .= '</tr>';
			
		}
		
	}
	////如果消費者有購買裸包袋茶商品，才顯示商品資訊。
	if(! empty($html_teaGroup1_content))
	{
		$html_teaGroup1 .= '<h2>裸包袋茶</h2>';
		$html_teaGroup1 .= '<table cellpadding="0" cellspacing="0" width="915" bordercolor="#BEA787" border="1">';
		$html_teaGroup1 .= '<tr bgcolor="#E2D7C9">';
		$html_teaGroup1 .= '<th width="455" align="left" style="padding-left:47px;">產品名稱</th>';
		$html_teaGroup1 .= '<th width="122">價格</th>';
		$html_teaGroup1 .= '<th width="128">數量</th>';
		$html_teaGroup1 .= '<th>金額</th>';
		$html_teaGroup1 .= '</tr>';
		$html_teaGroup1 .= $html_teaGroup1_content;
		$html_teaGroup1 .= '</table>';
	}
	
	

	/*
	 * 填入『隨身袋茶』的商品資料，如果消費者沒有購買的商品就不要顯示出來。
	 */
	$html_teaGroup2 = '';
	$html_teaGroup2_content = '';
	//此讀取csv檔的函式被定義在readCsv.php裡頭。
	$ary_products_teaGroup2 = getFromCSV('cartree_products_group2.csv');
	for($i=1 ; $i<=8 ; $i++)
	{
		if(! empty($_POST['length2-' . $i]))
		{
			$html_teaGroup2_content .= '<tr>';
			$html_teaGroup2_content .= '<td align="left"  style="padding-left:47px;">' . $ary_products_teaGroup2[$i-1][0] . '</td>';
			$html_teaGroup2_content .= '<td class="money">' . $ary_products_teaGroup2[$i-1][1] . '</td>';
			$html_teaGroup2_content .= '<td><input type="text" name="length2-1" class="length" value="' . $_POST['length2-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup2_content .= '<td><input type="text" name="price2-1" class="price" value="' . $_POST['price2-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup2_content .= '</tr>';
			
		}
		
	}
	////如果消費者有購買隨身袋茶商品，才顯示商品資訊。
	if(! empty($html_teaGroup2_content))
	{
		$html_teaGroup2 .= '<h2>隨身袋茶</h2>';
		$html_teaGroup2 .= '<table cellpadding="0" cellspacing="0" width="915" bordercolor="#BEA787" border="1">';
		$html_teaGroup2 .= '<tr bgcolor="#E2D7C9">';
		$html_teaGroup2 .= '<th width="455" align="left" style="padding-left:47px;">產品名稱</th>';
		$html_teaGroup2 .= '<th width="122">價格</th>';
		$html_teaGroup2 .= '<th width="128">數量</th>';
		$html_teaGroup2 .= '<th>金額</th>';
		$html_teaGroup2 .= '</tr>';
		$html_teaGroup2 .= $html_teaGroup2_content;
		$html_teaGroup2 .= '</table>';
	}
	
	
	
	/*
	 * 填入『罐裝散茶』的商品資料，如果消費者沒有購買的商品就不要顯示出來。
	 */
	$html_teaGroup3 = '';
	$html_teaGroup3_content = '';
	//此讀取csv檔的函式被定義在readCsv.php裡頭。
	$ary_products_teaGroup3 = getFromCSV('cartree_products_group3.csv');
	for($i=1 ; $i<=9 ; $i++)
	{
		if(! empty($_POST['length3-' . $i]))
		{
			$html_teaGroup3_content .= '<tr>';
			$html_teaGroup3_content .= '<td align="left"  style="padding-left:47px;">' . $ary_products_teaGroup3[$i-1][0] . '</td>';
			$html_teaGroup3_content .= '<td class="money">' . $ary_products_teaGroup3[$i-1][1] . '</td>';
			$html_teaGroup3_content .= '<td><input type="text" name="length3-1" class="length" value="' . $_POST['length3-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup3_content .= '<td><input type="text" name="price3-1" class="price" value="' . $_POST['price3-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup3_content .= '</tr>';
			
		}
		
	}
	////如果消費者有購買罐裝散茶商品，才顯示商品資訊。
	if(! empty($html_teaGroup3_content))
	{
		$html_teaGroup3 .= '<h2>罐裝散茶</h2>';
		$html_teaGroup3 .= '<table cellpadding="0" cellspacing="0" width="915" bordercolor="#BEA787" border="1">';
		$html_teaGroup3 .= '<tr bgcolor="#E2D7C9">';
		$html_teaGroup3 .= '<th width="455" align="left" style="padding-left:47px;">產品名稱</th>';
		$html_teaGroup3 .= '<th width="122">價格</th>';
		$html_teaGroup3 .= '<th width="128">數量</th>';
		$html_teaGroup3 .= '<th>金額</th>';
		$html_teaGroup3 .= '</tr>';
		$html_teaGroup3 .= $html_teaGroup3_content;
		$html_teaGroup3 .= '</table>';
	}
	
	
	
	/*
	 * 填入『禮盒組』的商品資料，如果消費者沒有購買的商品就不要顯示出來。
	 */
	$html_teaGroup4 = '';
	$html_teaGroup4_content = '';
	//此讀取csv檔的函式被定義在readCsv.php裡頭。
	$ary_products_teaGroup4 = getFromCSV('cartree_products_group4.csv');
	for($i=1 ; $i<=6 ; $i++)
	{
		if(! empty($_POST['length4-' . $i]))
		{
			$html_teaGroup4_content .= '<tr>';
			$html_teaGroup4_content .= '<td align="left"  style="padding-left:47px;">' . $ary_products_teaGroup4[$i-1][0] . '</td>';
			$html_teaGroup4_content .= '<td class="money">' . $ary_products_teaGroup4[$i-1][1] . '</td>';
			$html_teaGroup4_content .= '<td><input type="text" name="length4-1" class="length" value="' . $_POST['length4-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup4_content .= '<td><input type="text" name="price4-1" class="price" value="' . $_POST['price4-' . $i] . '" readonly="readonly" /></td>';
			$html_teaGroup4_content .= '</tr>';
			
		}
		
	}
	////如果消費者有購買禮盒組商品，才顯示商品資訊。
	if(! empty($html_teaGroup4_content))
	{
		$html_teaGroup4 .= '<h2>禮盒組</h2>';
		$html_teaGroup4 .= '<table cellpadding="0" cellspacing="0" width="915" bordercolor="#BEA787" border="1">';
		$html_teaGroup4 .= '<tr bgcolor="#E2D7C9">';
		$html_teaGroup4 .= '<th width="455" align="left" style="padding-left:47px;">產品名稱</th>';
		$html_teaGroup4 .= '<th width="122">價格</th>';
		$html_teaGroup4 .= '<th width="128">數量</th>';
		$html_teaGroup4 .= '<th>金額</th>';
		$html_teaGroup4 .= '</tr>';
		$html_teaGroup4 .= $html_teaGroup4_content;
		$html_teaGroup4 .= '</table>';
	}
	
	//收件人 - 同訂購人
	$chk_sameBox = isset($_POST['sameBox'])?'checked':'';
	
	//可收件時間 - 中午前
	$chk_deliverDate_1 = isset($_POST['deliverDate1'])?'checked':'';
	
	//可收件時間 - 下午12時-17時
	$chk_deliverDate_2 = isset($_POST['deliverDate2'])?'checked':'';
	
	//可收件時間 - 下午17時-20時
	$chk_deliverDate_3 = isset($_POST['deliverDate3'])?'checked':'';
	
	//付款方式
	if(isset($_POST['payMethod']))
	{
		$rdo_payMethod_1 = ('1' === $_POST['payMethod'])?'checked':'';
		$rdo_payMethod_2 = ('2' === $_POST['payMethod'])?'checked':'';
		$rdo_payMethod_3 = ('3' === $_POST['payMethod'])?'checked':'';
	}
	else 
	{
		$rdo_payMethod_1 = '';
		$rdo_payMethod_2 = '';
		$rdo_payMethod_3 = '';
	}

	//發票開立資料
	if(isset($_POST['invoice']))
	{
		$rdo_invoice_1 = ('1' === $_POST['invoice'])?'checked':'';
		$rdo_invoice_2 = ('2' === $_POST['invoice'])?'checked':'';
	}
	else 
	{
		$rdo_invoice_1 = '';
		$rdo_invoice_2 = '';
	}
	
	//發票郵寄地址 - 同訂購人
	$chk_sameBox2 = isset($_POST['sameBox2'])?'checked':'';
		
	
	
	
$body = <<<BODY
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>ARCTREE 茶話心語 訂購單</title>
<style>
* {
	margin:0;
	padding:0;	
}
#order-container {
	width:915px;
	margin:0 auto;	
}
#order-container h2 {
	background:#BEA787;	
	font-size:18px;
	font-weight:bold;	
	line-height:33px;
	color:#FFFFFF;
	padding-left:47px;
	margin-top:26px;		
}
#order-container table {
	letter-spacing:3px;
	vertical-align:middle;
}
#order-container th {
	font-size:15px;
	font-weight:bold;	
	color:#BEA787;	
	line-height: 25px;
}
#order-container td {
	font-size:14px;
	font-weight:bold;
	color:#727171;	
	line-height:31px;
}
#order-container table span {
	font-size:12px;
	letter-spacing:0;
	color:#BEA787;	
}
#order-container .length {
	width:113px;
	text-align:center;			
}
#order-container .price {
	width:144px;
	text-align:center;	
}
#clip {
	position:fixed;
	width:100%;
	height:100%;
	background:#000;
	opacity:0.7;
	left:0;
	top:0;
	z-index:1;
}
#info {
	position:fixed;
	padding:30px 50px;
	width:100px;
	height:20px;
	line-height:20px;
	color:#000;
	background:#CCC;
	left:50%;
	top:50%;
	margin-left:-100px;
	margin-top:-40px;
	z-index:3;
	font-size:14px;
}
</style>
</head>
<body>
<div id="wrapper">  
    <div class="header">
            <div class="clear"></div>
    </div>
	<div id="order-container">
    	<form name="orderForm" id="orderForm" method="post" action="sendMail.php">
    	
    	{$html_teaGroup1}

		{$html_teaGroup2}                                                                          

        {$html_teaGroup3}
                                                                              
        {$html_teaGroup4}
		  
		<table cellpadding="0" cellspacing="0" width="915" bordercolor="#BEA787" border="1" style="margin-top:10px;">
            <tr align="center">
            	<td align="left" width="711" style="padding-left:47px;">購買商品合計</td>
                <td><input type="text" name="productPrice" id="productPrice" size="21" readonly="readonly" style="text-align:center;" value="{$_POST['productPrice']}" /></td>
            </tr>
            <tr align="center">
            	<td align="left" style="padding-left:47px;">運費計算 (滿2000免運費，未滿兩千酌收運費100元)</td>
                <td><input type="text" name="shippingPrice" id="shippingPrice" size="21" readonly="readonly" style="text-align:center;" value="{$_POST['shippingPrice']}" /></td>
            </tr>
            <tr align="center">
            	<td align="left" style="padding-left:47px;">貨到付款酌收手續費30元</td>
                <td><input type="text" name="cashOn" id="cashOn" size="21" readonly="readonly" style="text-align:center;" value="{$_POST['cashOn']}" /></td>
            </tr>
			<tr bgcolor="#E2D7C9">
				<td align="left" style="padding-left:47px;">金額加總</td>
				<td align="center"><input type="text" name="totalPrice" id="totalPrice" class="total" size="21" readonly="readonly" style="text-align:center;" value="{$_POST['totalPrice']}" /></td>
			</tr>                                                                                    
		</table>      
        <h2>訂購資訊</h2>   
		<table cellpadding="0" cellspacing="0" width="915" bordercolor="#BEA787" border="1" style="margin-top:10px;">   
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>訂購日期：</label>
                    <input type="text" name="orderDate" id="orderDate" size="19" value="{$_POST['orderDate']}" readonly="readonly" />
                </td>
            </tr>
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>訂購人：</label>
                    <input type="text" name="order" id="order" size="19" value="{$_POST['order']}" readonly="readonly" /> 
                    <span class="gold">(必填)</span>    
                	<label>連絡電話：</label>
                    <input type="text" name="tel" id="tel" size="19" value="{$_POST['tel']}" readonly="readonly" /> 
                    <span class="gold">(必填)</span>  
                	<label>行動電話：</label>
                    <input type="text" name="phone" id="phone" size="19" value="{$_POST['phone']}" readonly="readonly" /> 
                    <span class="gold">(必填)</span>                                                     
                </td>
            </tr>
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>Email：</label>
                    <input type="text" name="email" id="email" size="19" value="{$_POST['email']}" readonly="readonly" />                
                </td>
            </tr>
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>收件人：</label>
                    <input type="text" name="deliver" id="deliver" size="19" value="{$_POST['deliver']}" readonly="readonly" />
                    <input type="checkbox" name="sameBox" id="sameBox" {$chk_sameBox} disabled />
                    <label for="sameBox">同訂購人 (其他資訊免填)</label>
                </td>
            </tr>
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>Email：</label>
                    <input type="text" name="email2" id="email2" size="19" value="{$_POST['email2']}" readonly="readonly" />  
                    <span class="gold">(必填)</span>    
                	<label>連絡電話：</label>
                    <input type="text" name="tel2" id="tel2" size="19" value="{$_POST['tel2']}" readonly="readonly" /> 
                    <span class="gold">(必填)</span>  
                	<label>行動電話：</label>
                    <input type="text" name="phone2" id="phone2" size="19" value="{$_POST['phone2']}" readonly="readonly" /> 
                    <span class="gold">(必填)</span>                                                     
                </td>
            </tr>
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>收件地址：</label>
                    <input type="text" name="postal" id="postal" size="8" placeholder="郵遞區號" maxlength="3" value="{$_POST['postal']}" readonly="readonly" />
                    <input type="text" name="adress" id="adress" size="101" value="{$_POST['adress']}" readonly="readonly" />
                    <span class="gold">(必填)</span>                
                </td>
            </tr>
        	<tr>
            	<td align="left" style="padding-left:47px;">
                	<label>可收件時間：</label>
                    <input type="checkbox" name="deliverDate1" value="1" {$chk_deliverDate_1} disabled />中午前
                    <input type="checkbox" name="deliverDate2" value="2" {$chk_deliverDate_2} disabled />下午12時-17時
                    <input type="checkbox" name="deliverDate3" value="3" {$chk_deliverDate_3} disabled />下午17時-20時
                </td>
            </tr>
        	<tr>
            	<td>
                	<div class="formTop" style="padding-left:47px;border-bottom:2px dotted #BCA586;">
                    	<p>付款方式：</p>
                        <p>
                        	<label>總金額：</label>
                            <input type="text" name="total" id="total" class="total" size="19" value="0" readonly="readonly" />
                        </p>
                        <p>
                        	<input type="radio" name="payMethod" id="payMethod1" value="1" {$rdo_payMethod_1} disabled />
                        	<label>郵局劃撥，轉出帳號末五碼：</label>
                            <input type="text" name="lastCode1" size="19" value="{$_POST['lastCode2']}" readonly="readonly" />
                        </p>
                        <p>劃撥帳號：19083801</p>
                        <p>郵局戶名：徐淑芬</p>
                        <p>
                        	<input type="radio" name="payMethod" id="payMethod2" value="2" {$rdo_payMethod_2} disabled />
                        	<label>ATM轉帳，轉出帳號末五碼：</label>
                            <input type="text" name="lastCode2" size="19" value="{$_POST['lastCode2']}" readonly="readonly" />                        	
                        </p>
                        <p>銀行名稱：聯邦商業銀行 龍潭分行    銀行代碼：803</p>
                        <p>銀行帳號：030508014698             戶名：葉步真</p>
                        <p>
                        	<input type="radio" name="payMethod" id="payMethod3" value="3" {$rdo_payMethod_3} disabled />
                        	<label>貨到付款 (酌收30元手續費)</label>                        
                        </p>
                    </div>
                    <div class="formBtm" style="padding-left:47px;">
                    	<p>發票開立資料：</p>
                        <p>
                        	<input type="radio" name="invoice" id="nvoice2" value="1" {$rdo_invoice_1} disabled />
                        	<label>二聯式</label>                          
                        </p>
                        <p>
                        	<input type="radio" name="invoice" id="nvoice3" value="2" {$rdo_invoice_2} disabled />
                        	<label>二聯式</label> 
                            <label>發票抬頭：</label>
                            <input type="text" name="invoiceTitle" size="26" value="{$_POST['invoiceTitle']}" readonly="readonly" /> 
                            <label>統一編號：</label>
                            <input type="text" name="uniform" size="26" value="{$_POST['uniform']}" readonly="readonly" />                                                             
                        </p>                        
                        <p>
                        	<label>發票郵寄地址：</label> 
                            <input type="checkbox" name="sameBox2" id="sameBox2" {$chk_sameBox2} disabled /><label for="sameBox2">同訂購人</label>                            
                        </p>
                        <p style="padding-left:130px;">
                            <input type="text" name="postal2" id="postal2" size="8" placeholder="郵遞區號" maxlength="3" value="{$_POST['postal2']}" readonly="readonly" />
                            <input type="text" name="adress2" id="adress2" size="100" value="{$_POST['adress2']}" readonly="readonly" />                        	
                        </p>
                    </div>
                </td>
            </tr>                                                                                    
        </table>
        <div style="margin-top:20px;text-align:center;">
        </div>
        </form>                         	
	</div>
    <div class="footer">
      <h1>新福隆股份有限公司 SHINGFULUNG CO., LTD.</h1>
      <p>桃園縣龍潭鄉龍新路三和段500巷25號</p>
      <p>Tel:+886-3-4792008  Fax:+886-3-4892991</p>
      <div class="finpo">©2012  Arctree Design All Rights Reserved.<a href="http://www.finpo.com.tw/" target="_blank">Web Design By Finpo.</a></div>
    </div>
</div>
</body>
</html>
BODY;

$mail= new PHPMailer(); //建立新物件
$mail->IsSMTP(); //設定使用SMTP方式寄信
$mail->SMTPAuth = true; //設定SMTP需要驗證
$mail->SMTPSecure = "ssl"; // Gmail的SMTP主機需要使用SSL連線
$mail->Host = "smtp.gmail.com"; //Gamil的SMTP主機
$mail->Port = 465;  //Gamil的SMTP主機的埠號(Gmail為465)。
$mail->CharSet = "utf-8"; //郵件編碼

$mail->Username = "service@arctree.com.tw"; //Gamil帳號
$mail->Password = "arctreeadmin"; //Gmail密碼

$mail->From = 'service@arctree.com.tw'; //寄件者信箱
$mail->FromName = '客服信箱'; //寄件者姓名

$mail->Subject = '訂購單 - ' . $_POST['order'];  //郵件標題
$mail->Body = $body; //郵件內容


$mail->IsHTML(true); //郵件內容為html ( true || false)
$mail->AddAddress('service@arctree.com.tw', '茶話心語'); //寄給客服信箱

$mail->AddAddress($_POST['email'], $_POST['order']); //寄給訂購人信箱

if( $_POST['email2'] != $_POST['email'] ){
	$mail->AddAddress($_POST['email2'], $_POST['deliver']); //如果收件人信箱和訂購人信箱不同，寄給收件人信箱
}

if(!$mail->Send()) {
	echo "發送錯誤: " . $mail->ErrorInfo;
} else {
	//如果訂購單寄送成功，重新導頁至前一頁。
	header('Location: ' . $_SERVER['HTTP_REFERER']);
}


